<?php
    if(isset($_GET['id']))
    {
        $id = $_GET['id'];
        require_once('../Database/Database.php');
        $db = new Database();
        $db->Connect();
        $shortcut = $db->SelectColumns('Series', 'Shortcut', 'ID_Series = ' . $id);
        if($shortcut)
        {
            $shortcut = $shortcut[0]->Shortcut;

            function GetVehicles($team_id, $shortcut)
            {
                $vehicles = array();

                require_once('../Database/Database.php');
                $db = new Database();
                $db->Connect();

                $drivers = $db->SelectColumns($shortcut . '_drivers', 'Vehicle_ID', 'Team_ID = ' . $team_id);
                foreach($drivers as $driver)
                {
                    $vehicle = $db->Select($shortcut . '_vehicles', 'ID_Vehicle = ' . $driver->Vehicle_ID);
                    if(!in_array($vehicle[0]->Name, $vehicles))
                    {
                        $vehicles[] = $vehicle[0]->Name;
                    }
                }
                $db->Disconnect();
                return $vehicles;
            }
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Teams | Ranking - ALPHA</title>
    </head>
    <body>
        <div id="header">Teams</div>
        <div id="main">
            <table>
                <tr>
                    <td>Name</td>
                    <td>Fahrzeug</td>
                </tr>
                <?php
                    $teams = $db->Select($shortcut . '_teams');
                    $db->Disconnect();

                    foreach($teams as $team)
                    {
                        echo '<tr class="team">';

                        echo '<td>' . $team->Name . '</td>';
                        $vehicles = GetVehicles($team->ID_Team, $shortcut);
                        echo '<td>';
                        echo implode(', ', $vehicles);
                        echo '</td></tr>';
                    }
                ?>
            </table>
        </div>
        <div id="footer"></div>

    </body>
</html>
<?php
        }
        else
        {
            echo 'Ung&uuml;ltige Serien ID.';
        }
    }
    else
    {
        echo 'ERROR';
    }
?>